<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table
        :data="orderlist"
        border
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="#"
        ></el-table-column>

        <el-table-column
          prop="order_number"
          label="订单编码"
        >
        </el-table-column>

        <el-table-column
          prop="order_price"
          label="订单价格"
          width="90"
        >
        </el-table-column>
        <el-table-column
          prop="pay_status"
          label="是否付款"
          width="90"
        >
          <template slot-scope="scope">
            <!-- {{ scope.row.pay_status }} -->
            <el-tag
              size="mini"
              type="success"
              v-if="scope.row.pay_status === 1"
            >已发货</el-tag>
            <el-tag
              size="mini"
              type="danger"
              v-else
            >未发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="90"
        >
        </el-table-column>
        <el-table-column
          prop="update_time"
          label="下单时间"
          width="180"
        >
          <template slot-scope="scope">
            {{ scope.row.update_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column
          prop="order_price"
          label="操作"
          width="120"
        >
          <template>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showBox"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location-information"
              size="mini"
              @click="showLocation"
            ></el-button>
          </template>
        </el-table-column>

      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

    </el-card>

    <!-- 修改地址 弹窗 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
    >

      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item
          label="省市区/县"
          prop="address1"
        >
          <el-cascader
            :options="cityData"
            v-model="addressForm.address1"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>

        </el-form-item>
        <el-form-item
          label="详细地址"
          prop="address2"
        >
          <el-input v-model="addressForm.name"></el-input>
        </el-form-item>

      </el-form>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogVisible = false"
        >确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息 -->
    <el-dialog
      title="物流动态"
      width="50%"
      :visible.sync="locationVisible"
    >

      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(item, i) in kuaidiData"
          :key="i"
          :timestamp="item.time"
        >
          {{item.context}}
        </el-timeline-item>
      </el-timeline>

    </el-dialog>

  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  name: '',
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: [],
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: ''
      },
      orderlist: [],
      total: 0,
      addressVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择城市', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详情地址', trigger: 'blur' }
        ]
      },
      cityData,
      // 物流
      locationVisible: false,
      kuaidiData: [],
      reverse: false

    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$axios.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.orderlist = res.data.goods
      this.total = res.data.total
      console.log(this.queryInfo)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryInfo.pagesize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryInfo.pagenum = val
      this.getOrderList()
    },
    showBox() {
      this.addressVisible = true
    },
    showLocation() {
      this.locationVisible = true
      this.getKuaidiData()
    },
    // 获取 物流动态
    async getKuaidiData() {
      console.log(this.kuaidiData)
      const { data: res } = await this.$axios.get(`/kuaidi/1106975712662`)

      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.message)
      }
      console.log(res.data)
      this.kuaidiData = res.data

      console.log(this.kuaidiData)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>